package com.hongawen.poi.service;

import cn.afterturn.easypoi.csv.CsvExportUtil;
import cn.afterturn.easypoi.csv.entity.CsvExportParams;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.core.util.CharsetUtil;
import com.hongawen.poi.pojo.Course;
import com.hongawen.poi.pojo.Student;
import com.hongawen.poi.pojo.Teacher;
import com.hongawen.poi.util.HttpServletUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author hongawen
 * @version 1.0.0
 * @date 2022年03月30日 15:53
 */
@Slf4j
@Component
public class StudentService {
    private static List<Student> students = new ArrayList<>();
    private static List<Teacher> teachers = new ArrayList<>();
    private static List<Course> courses = new ArrayList<>();

    static {
        Student student;
        for (int i = 1; i < 6; i++) {
            student = new Student();
            student.setName("小明" + i + "号");
            student.setBirthday(new Date());
            student.setRegistrationDate(LocalDateTime.now());
            student.setSex(i % 2);
            student.setHome(i % 3);
            students.add(student);
        }

        students = students.stream().sorted(Comparator.comparingInt(Student::getHome)).collect(Collectors.toList());

        Teacher teacher;
        Course course;
        for (int i = 1; i < 4; i++) {
            teacher = new Teacher();
            course = new Course();
            teacher.setName("小王老师");
            teacher.setAge(35 + i);
            teacher.setStudents(students);
            course.setTeacher(teacher);
            course.setStudents(students);
            course.setName("玉女心经" + i + "篇");
            courses.add(course);
            teachers.add(teacher);
        }


    }

    public void exportStudent() {
        try {
            HttpServletResponse response = HttpServletUtil.getResponse();
            String fileName = URLEncoder.encode("hello.xls", CharsetUtil.UTF_8);
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            response.setContentType("application/octet-stream;charset=UTF-8");
            ServletOutputStream outputStream = response.getOutputStream();
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("计算机一班学生", "学生"), Student.class, students);
            workbook.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            log.error(">>> 导出数据异常：{}", e.getMessage());
        }
    }

    public void exportStudentCSV() {
        try {
            HttpServletResponse response = HttpServletUtil.getResponse();
            String fileName = URLEncoder.encode("hello.csv", CharsetUtil.UTF_8);
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            response.setContentType("application/octet-stream;charset=UTF-8");
            ServletOutputStream outputStream = response.getOutputStream();
            CsvExportParams csvExportParams = new CsvExportParams();
            csvExportParams.setEncoding(CharsetUtil.GBK);

            CsvExportUtil.exportCsv(csvExportParams, Student.class, students, outputStream);
            outputStream.close();
        } catch (IOException e) {
            log.error(">>> 导出数据异常：{}", e.getMessage());
        }
    }

    public void exportTeacher() {
        try {
            HttpServletResponse response = HttpServletUtil.getResponse();
            String fileName = URLEncoder.encode("hello.xls", CharsetUtil.UTF_8);
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            response.setContentType("application/octet-stream;charset=UTF-8");
            ServletOutputStream outputStream = response.getOutputStream();
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), Teacher.class, teachers);
            workbook.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            log.error(">>> 导出数据异常：{}", e.getMessage());
        }
    }


    public void exportCourse() {
        try {
            HttpServletResponse response = HttpServletUtil.getResponse();
            String fileName = URLEncoder.encode("hello.xls", CharsetUtil.UTF_8);
            response.reset();
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            response.setContentType("application/octet-stream;charset=UTF-8");
            ServletOutputStream outputStream = response.getOutputStream();
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), Course.class, courses);
            workbook.write(outputStream);
            outputStream.close();
        } catch (IOException e) {
            log.error(">>> 导出数据异常：{}", e.getMessage());
        }
    }
}
